/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parser.part.n2;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Patronus
 */
interface TableMethods {

    public int addRule(int line, Object rule);

    public int setRule(int line, int pos, Object rule);

    public List getTable();

    public void addLines(int i);

    public void addLine();
}

class Creator implements TableMethods {

    private List table;

    public Creator() {
        table = new ArrayList();

    }

    @Override
    public List getTable() {
        return this.table;
    }

    @Override
    public int addRule(int line, Object rule) {
        if (this.table.size() > line) {

            return addRuleImmediatly(line, rule);
        } else {
            addLines(line - this.table.size() + 1);
            addRuleImmediatly(line, rule);
        }
        return -1;
    }

    int addRuleImmediatly(int line, Object rule) {
        List ls = getListOnLine(line);
        ls.add(rule);
        setListOnLine(line, ls);
        return ls.size();
    }

    @Override
    public int setRule(int line, int pos, Object rule) {
        if (table.size() > line) {
            List list = getListOnLine(line);
            return 0;
        } else {
            return -1;
        }


    }

    @Override
    public void addLines(int i) {
        for (int n = 0; n < i; n++) {
            this.table.add(new ArrayList());
        }

    }

    @Override
    public void addLine() {
        this.table.add(new ArrayList());

    }

    private List getListOnLine(int line) {
        if (this.table.size() > line) {
            return (List) this.table.get(line);
        } else {
            return null;
        }


    }

    private void setListOnLine(int line, List ls) {
        if (this.table.size() > line) {
            this.table.set(line, ls);
        }
    }
}
